package com.yaoyanshe.trialfield.common.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.yaoyanshe.trialfield.common.R;


public abstract class BaseFragment extends Fragment {

    protected Context mContext;
    private View mView;

    protected boolean isFistInto = true;

    public String TAG = getClass().getSimpleName();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        onAttachToContext(context);
    }

    private void onAttachToContext(Context context) {
        mContext = context;
        Bundle arguments = getArguments();
        if (arguments != null) {
            initData(arguments);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFistInto = true;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(getLayoutId(), container, false);
        }
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setViewData();
        setListener(view);
    }


    /**
     * 返回布局
     *
     * @return 布局id
     */
    @LayoutRes
    protected abstract int getLayoutId();

    public void initData(@NonNull Bundle arguments) {

    }

    public void initView(View view) {

    }

    public void setListener(View view) {

    }

    public void setViewData() {

    }

    protected void hideSoftKeyBord() {
        if (getActivity() != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
            }
        }
    }

    protected void openActivityAnimation() {
        if (getActivity() != null) {
            getActivity().overridePendingTransition(R.anim.right_in, R.anim.left_out);
        }
    }


    protected void closeActivity() {
        if (getActivity() != null) {
            getActivity().finish();
            getActivity().overridePendingTransition(R.anim.left_in, R.anim.right_out);
        }
    }


}
